const app = getApp();
const { isAdminRole } = require('../../../api/common');

Page({
  data: {
    flag: false,
    show: false,
    msgList: [],
    input: '',
    isData: false,
  },

  async onLoad(opts) {
    const isAdmin = await isAdminRole();
    let { from_openid, to_openid, message_id = '', name } = opts;
    this.setData({ from_openid, to_openid, message_id, name: decodeURIComponent(name), isAdmin }, () => {
      this.getMessageList();
    });
  },
  onShow() {
    let systemInfo = wx.getSystemInfoSync();
    let ios = !!(systemInfo.system.toLowerCase().search('ios') + 1);
    console.info('---------ios-----------', ios);
    this.setData({
      ios
    })
  },

  onMark(e) {
    let { btntype = '', message_id = '' } = e.currentTarget.dataset;
    let { flag } = this.data;
    this.setData({ flag: !flag, focus: true, btntype, message_id });
  },

  getInput(e) {
    this.setData({ input: e.detail.value });
  },

  sendMsg(e) {
    let { input: content, to_openid, btntype, message_id } = this.data;
    let spaceContentCheck = new RegExp('^[ ]+$'); // 空格串判断
    let data;
    if ((content === '' || spaceContentCheck.test(content)) && e) {
      wx.showToast({
        title: '内容不能为空',
        icon: 'none',
        mask: true,
        duration: 1500,
      });
      return;
    }
    if (btntype === 'add') {
      data = {
        content,
        message_id: '',
        to_openid,
        bindacid: wx.getStorageSync('cor_uniacid') || 0,
      };
    } else {
      data = {
        content,
        message_id,
        to_openid,
        bindacid: wx.getStorageSync('cor_uniacid') || 0,
      };
    }
    app.util.request({
      url: 'message/api/jpLeaveMessage',
      data,
      success: res => {
        wx.showToast({
          title: res.data.message,
          icon: res.data.errno ? 'none' : 'success',
          mask: true,
          duration: 1500,
        });
        this.setData(
          {
            flag: false,
            input: '',
          },
          () => {
            res.data.errno === 0 && this.getMessageList();
          },
        );
      },
    });
  },

  getMessageList() {
    const { isAdmin } = this.data;
    let { to_openid, message_id } = this.data;
    let url = 'message/api/jpMessageList';
    let data = {};
    if (isAdmin) {
      data.to_openid = to_openid;
    } else {
      data.m_id = message_id;
    }
    data.bindacid = wx.getStorageSync('cor_uniacid') || 0,

    app.util.request({
      url,
      data,
      success: res => {
        if (res.data.errno === 0) {
          if (res.data.data.length > 0) {
            let msgList = res.data.data;
            wx.setNavigationBarTitle({
              title: msgList[0].name + '的留言' || '留言',
            });
            msgList.length &&
              msgList.forEach((v, i) => {
                if (v.reply.length) {
                  v.reply.forEach((item, index) => {
                    if (item.openid === to_openid) {
                      v.no_reply = false;
                    } else {
                      v.no_reply = true;
                    }
                  });
                } else {
                  v.no_reply = true;
                }
              });
            this.setData({ msgList, show: true, isData: true });
            wx.hideLoading();
          } else {
            this.setData({ msgList: [], show: false, isData: true });
            wx.hideLoading();
          }
        }
      },
      fail: res => {
        this.setData({ msgList: [], show: false, isData: true });
        wx.hideLoading();
      },
    });
  },
});
